import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.IllegalModeException;
import javax.microedition.lcdui.game.Sprite;





public class ScrollCanvas extends Canvas {
	private ImageViewer midlet;
	private Image im;
	private Image imoriginal;
	//private Command salir;
	//private Command zoom;
	private int x, y,w,h;
	FileConnection fconn;
	private int zoomlevel;
	private boolean rotate=false;
	int xscroll=30; //x scroll value
	int yscroll=30; //y scroll value



  public ScrollCanvas(ImageViewer mid,String filename){
	//zoom = new Command("Zoom",Command.SCREEN,1);
	//this.addCommand(zoom);
	this.midlet = mid;
	final String filen=filename;
	setFullScreenMode(true);
   //  this.addCommand(salir);
   //  this.setCommandListener(this);
//	Thread t = new Thread() {
  //      public void run() {
        

	try {
            fconn = (FileConnection) Connector.open(filen);


            InputStream in = fconn.openInputStream();
            im = Image.createImage(in);
	
            in.close();
            fconn.close();
        }
        catch (IOException e1) {
		                System.out.println("ioexception");
        }
        catch (IllegalModeException e2) {
		                System.out.println("imexception");
        }
        catch (java.lang.SecurityException e3) {
		                System.out.println("secexception");
        }          

//	}	 
  //    }; 
    //  t.start();       

	x = 0;
	y = 0;
	imoriginal=Image.createImage(im);
	w=im.getWidth();
	h=im.getHeight();
	
  }
  public void paint(Graphics g) {
	String zm="";
	g.setColor(255,255,255);
	g.fillRect(0,0,getWidth(),getHeight());
	g.setColor(0,0,0);
	g.drawImage(im,x,y,Graphics.TOP|Graphics.LEFT);
	switch(zoomlevel){
		case 1:{zm="100%";break;}
		case 2:{zm="75%";break;}
		case 3:{zm="50%";break;}
		case 4:{zm="25%";break;}
		case 5:	{zm="fit to width";break;}
	}
	g.drawString("zoom: "+zm,0,0,Graphics.TOP|Graphics.LEFT);

	
    }

	protected void finalize()
	{
	}

 
	protected void keyRepeated(int keyCode) {
		switch(getGameAction(keyCode)){
			case Canvas.UP: { if(y!=0)y = y+30;
			break; }
			case Canvas.RIGHT: { int tmpscroll=im.getWidth()-(getWidth()+Math.abs(x));
				//System.out.println(xscroll);
				if(tmpscroll>0){
					if(tmpscroll>xscroll)x=x-xscroll;
					else x = x-xscroll;
				}
                        break;}
			case Canvas.DOWN: { if(y+im.getHeight()>getHeight())y = y-30;
			break;}
			case Canvas.LEFT:{ 
				if(x!=0){
					
					if(Math.abs(x)>xscroll)x=x+xscroll;
					else x = 0;
					
				}
                        break;}
		}
		this.repaint();
	}

	protected void keyPressed(int keyCode) {
      switch(getGameAction(keyCode)){
          case Canvas.UP: { if(y!=0)y = y+30;
                        break; }
          case Canvas.RIGHT: { int tmpscroll=im.getWidth()-(getWidth()+Math.abs(x));
				//System.out.println(xscroll);
				if(tmpscroll>0){
					if(tmpscroll>xscroll)x=x-xscroll;
					else x = x-xscroll;
				}
                        break;}
          case Canvas.DOWN: { if(y+im.getHeight()>getHeight())y = y-30;
                        break;}
          case Canvas.LEFT:{ 
				if(x!=0){
					
					if(Math.abs(x)>xscroll)x=x+xscroll;
					else x = 0;
					
				}
                        break;}
      }
      this.repaint();
    }

    
	public void rescaleImage(int zoom)
	{
		/* zoomlevel
		1=100%
		2=75%
		3=50%
		4=25%
		5=fit to screen
		*/

		//
		zoomlevel=zoom;
		if (zoom==1){//return original image (fasterrrrrrrrrrrr)!!
			im=imoriginal;
			this.x=0;
			this.y=0;
			this.repaint();
			return;
		}else if(zoom==5){ //fit to screen
			zoom=this.w/getWidth();
		}

		
		
		int sourceWidth = imoriginal.getWidth();
		int sourceHeight = imoriginal.getHeight();
		int width=w/zoom;
		int height=h/zoom;
	
		Image newImage = Image.createImage(width, height);
		Graphics g = newImage.getGraphics();
		//Graphics gscreen = im.getGraphics();
		System.out.println("zoomlevel: "+zoom+" width: "+width+" height: "+height);
		//int dx=0;
		//int dy=0;
		int rgb[]=new int[1];
		int rgb2[]=new int[1];
		int rgb3[]=new int[1];
		int rgb4[]=new int[1];
		for(int y=0; y<height+1/2; y++)
		{

			
			for(int x=0; x<width+1/2; x++)
			{
				
				
				imoriginal.getRGB(rgb,0,1,x*zoom,y*zoom,1,1);
				imoriginal.getRGB(rgb2,0,1,(this.w-1)-(x*zoom),y*zoom,1,1);
				imoriginal.getRGB(rgb3,0,1,x*zoom,(this.h-1)-(y*zoom),1,1);
				imoriginal.getRGB(rgb4,0,1,(this.w-1)-(x*zoom),(this.h-1)-(y*zoom),1,1);
				g.drawRGB(rgb,0,0,x,y,1,1,false);
				g.drawRGB(rgb2,0,0,width-x,y,1,1,false);				
				g.drawRGB(rgb3,0,0,x,height-y,1,1,false);
				g.drawRGB(rgb4,0,0,width-x,height-y,1,1,false);
				
				//System.out.println("line x: "+x+" line y: "+y);
			}
		//gscreen.setColor(0);
		//gscreen.fillRect(0,50,y,10);
		this.repaint();
		//dy++;
		//dx=0;
		}
		System.out.println("zoom finish");
		
		im= newImage;
		this.x=0;
		this.y=0;
		this.repaint();
	}

	public void rotateImage(){
			
				int temp;
				Image newImage = Image.createImage(this.h,this.w);
				Graphics g = newImage.getGraphics();
				g.drawRegion(imoriginal,0,0,this.w,this.h,Sprite.TRANS_ROT90,0,0,Graphics.TOP|Graphics.LEFT);
				temp=this.w;
				this.w=this.h;
				this.h=temp;	
				im=newImage;	
				imoriginal=im;		
				this.repaint();
				this.rotate=true;
	}

//	void rescaleImage(Image temp, int x, int y, int x2, int y2) { 
/*	void rescaleImage(int zoom) {
		System.out.println("zoom1");
		int ini[] = new int[this.w*this.h]; 

		System.out.println("zoom2");
		int i=0,t2; 
		int x2,y2;
		int dy=0;
		int dx=0; 

	
		x2=w*zoom/100;
		y2=h*zoom/100;


		im.getRGB(ini,0,this.w,0,0,this.w,this.h); 
		System.out.println("zoom3");
		int out[] = new int[x2*y2]; 

		t2=this.w/x2; 
			for (int yy = 0; yy < y2; yy++) { 
				dy = yy *this.h/y2; 

				for (int xx = 0; xx < x2; xx++) { 
					dx = xx * t2; 
					System.out.println("iteracion "+xx);
					out[i++]=ini[(this.w*dy)+dx]; 
			} 
		System.out.println("zoom4");
		this.im = Image.createRGBImage(out,getWidth(),getHeight(),true); 
		this.repaint();
	}*/

}
